package coml.jiyun.hp.xaizai;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import coml.jiyun.hp.xaizai.adapter.MainAdapter;
import coml.jiyun.hp.xaizai.fragment.DownLoadFragment;
import coml.jiyun.hp.xaizai.fragment.HomeFragment;

public class MainActivity extends AppCompatActivity  {

    private Toolbar mMainTool;
    private ViewPager mMainVp;
    private TabLayout mMainTab;
    private ArrayList<Fragment> fragments;
    /**
     * 首页
     */
    private TextView mToolTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mMainTool = (Toolbar) findViewById(R.id.main_tool);
        mMainVp = (ViewPager) findViewById(R.id.main_vp);
        mMainTab = (TabLayout) findViewById(R.id.main_tab);

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new DownLoadFragment());

        mMainTool.setTitle("");
        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragments);
        mMainVp.setAdapter(mainAdapter);
        mMainTab.setupWithViewPager(mMainVp);

        setSupportActionBar(mMainTool);
        mMainTab.getTabAt(0).setText("首页").setIcon(R.mipmap.ic_launcher);
        mMainTab.getTabAt(1).setText("下载").setIcon(R.mipmap.ic_launcher);
        mMainTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int selectedTabPosition = mMainTab.getSelectedTabPosition();
                if (selectedTabPosition == 0) {
                    mToolTv.setText("首页");
                } else if (selectedTabPosition == 1) {
                    mToolTv.setText("下载");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mToolTv = (TextView) findViewById(R.id.tool_Tv);
    }

}
